package cn.iocoder.yudao.module.exam.service.aimarkingtask;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.exam.controller.admin.aimarkingtask.vo.*;
import cn.iocoder.yudao.module.exam.dal.dataobject.aimarkingtask.AiMarkingTaskDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.exam.dal.mysql.aimarkingtask.AiMarkingTaskMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.exam.enums.ExamErrorCodeConstants.AI_MARKING_TASK_NOT_EXISTS;

/**
 * AI批阅任务 Service 实现类
 *
 * @author francis
 */
@Service
@Validated
public class AiMarkingTaskServiceImpl implements AiMarkingTaskService {

    @Resource
    private AiMarkingTaskMapper aiMarkingTaskMapper;

    @Override
    public Long createAiMarkingTask(AiMarkingTaskSaveReqVO createReqVO) {
        // 插入
        AiMarkingTaskDO aiMarkingTask = BeanUtils.toBean(createReqVO, AiMarkingTaskDO.class);
        aiMarkingTaskMapper.insert(aiMarkingTask);

        // 返回
        return aiMarkingTask.getId();
    }

    @Override
    public void updateAiMarkingTask(AiMarkingTaskSaveReqVO updateReqVO) {
        // 校验存在
        validateAiMarkingTaskExists(updateReqVO.getId());
        // 更新
        AiMarkingTaskDO updateObj = BeanUtils.toBean(updateReqVO, AiMarkingTaskDO.class);
        aiMarkingTaskMapper.updateById(updateObj);
    }

    @Override
    public void deleteAiMarkingTask(Long id) {
        // 校验存在
        validateAiMarkingTaskExists(id);
        // 删除
        aiMarkingTaskMapper.deleteById(id);
    }

    @Override
        public void deleteAiMarkingTaskListByIds(List<Long> ids) {
        // 删除
        aiMarkingTaskMapper.deleteByIds(ids);
        }


    private void validateAiMarkingTaskExists(Long id) {
        if (aiMarkingTaskMapper.selectById(id) == null) {
            throw exception(AI_MARKING_TASK_NOT_EXISTS);
        }
    }

    @Override
    public AiMarkingTaskDO getAiMarkingTask(Long id) {
        return aiMarkingTaskMapper.selectById(id);
    }

    @Override
    public PageResult<AiMarkingTaskDO> getAiMarkingTaskPage(AiMarkingTaskPageReqVO pageReqVO) {
        return aiMarkingTaskMapper.selectPage(pageReqVO);
    }

}